package top.majuan.lianxi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.majuan.lianxi.base.PageBean;
import top.majuan.lianxi.base.R;
import top.majuan.lianxi.dao.TbBilloardDAO;
import top.majuan.lianxi.entity.TbBillboard;
import top.majuan.lianxi.service.TbBilloardService;
import top.majuan.lianxi.vo.PageVo;

@Service
@RequiredArgsConstructor
@Slf4j
public class TbBilloardServiceImpl implements TbBilloardService {
    public final TbBilloardDAO tbBillboardDAO;


    @Override
    public R<String> insert(TbBillboard tbBillboard) {
        int i = tbBillboardDAO.insert(tbBillboard);
        return i==1? R.ok("添加成功",null) : R.fail("添加失败",null);
        }
    @Override
    public R<TbBillboard> queryByid(Integer id) {
        TbBillboard tbClass = tbBillboardDAO.selectById(id);
        return tbClass == null?R.fail("查询失败",null) : R.ok(tbClass);

    }
    @Override
    public R<String> update(TbBillboard tbBillboard) {
        int i = tbBillboardDAO.updateById(tbBillboard);
        return i==1? R.ok("修改成功",null) : R.fail("修改失败",null);
    }
    @Override
    public R<String> delete(Integer id) {
        int i = tbBillboardDAO.deleteById(id);
        return i==1? R.ok("删除成功",null) : R.fail("删除失败",null);
    }
    @Override
    public R<PageVo<TbBillboard>> queryPage(PageBean pageBean, TbBillboard tbBillboard) {
        IPage<TbBillboard> page = new Page<>(pageBean.getPageNumber(),pageBean.getPageSize());
        QueryWrapper<TbBillboard> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(tbBillboard.getUsername()),"username",tbBillboard.getUsername());
        wrapper.like(StringUtils.hasText(tbBillboard.getTitle()),"title",tbBillboard.getTitle());
        page = tbBillboardDAO.selectPage(page,wrapper);
        return R.ok(page);
    }
    }

